<?php

	fta_registerModules(
		array(
			"FTAORM/_FTAORMCMSAsset"
		)
	);

	class FTAORMCMSAsset extends _FTAORMCMSAsset implements IteratorAggregate {

		protected $folderIterator;
		
		function __construct(Zend_Db_Adapter_Abstract $zdb){
			parent::__construct($zdb);
		}
		
		function getSubAssetWithName($folderName){
			 return static::getAssetWithNameAndParent($folderName, $this, $this->zdb);
		}
		
		function addFolder($folderName){
			return static::addFolderWithNameAndParent($folderName, $this, $this->zdb);
		}
		
		function resetFolderIterator(){
			$this->folderIterator = null;
		}
		
		function getIterator(){
			if (is_null($this->folderIterator)){
				$objects = static::getAssetsWithParent($this,$this->zdb);
				if (count($objects)>0)	{
					$documentCodes = array_map(function ($i){ return $i->documentcode;}, $objects);
					$this->folderIterator = new ArrayIterator(array_combine($documentCodes, $objects));
				} else {
					$this->folderIterator = new ArrayIterator();
				}
			}
			return $this->folderIterator;
		}

		
		static function addFolderWithNameAndParent($name, FTAORMCMSAsset $parent, Zend_Db_Adapter_Abstract $zdb ){
			
			//cehck for existing folder with the anme and parent first. 
			
			$existing = static::getAssetWithNameAndParent($name, $parent, $zdb);
			if (!is_null($existing) && $existing instanceof \FTAORMCMSAsset ){
				return $existing;
			}
			$c = get_called_class();
			$f = new $c($zdb);
			$f->name = $name;
			$f->parent = $parent->documentcode;
			$f->isFolder = 1;
			
			$saved = false;
			try {
				$saved = $f->save();
			} catch (Exception $e){}
			
			if ($saved) {
				return $f;
			}
			
			return null;
		}
		
		
		static function getAssetsWithNameAndParent($name, FTAORMCMSAsset $parent, Zend_Db_Adapter_Abstract $zdb){
            
			if (is_null($name)){
                return array();
            }

			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName), $m->fieldMap )
                ->from(array('md' => 'cmscontentmetadata'))
                ->where('md.documentcode = m.documentcode')
            	->where('documenttypecode = ?', $m->documenttypecode)
            	->where('categorycode = ?', $parent->getPrimaryKey())
            	->where('title = ?', $name);
	        	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);
            		
		}
		
		static function getAssetWithNameAndParent($name, FTAORMCMSAsset $parent, Zend_Db_Adapter_Abstract $zdb){
            
			$folders = static::getAssetsWithNameAndParent($name, $parent, $zdb);
			if (is_array($folders) && count($folders) > 0){
				return array_pop($folders);
			} 
			return null;
            		
		}
		
		static function getActiveAssetsWithParent(FTAORMCMSAsset $parent, Zend_Db_Adapter_Abstract $zdb){
		
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName), $m->fieldMap )
                ->from(array('md' => 'cmscontentmetadata'))
                ->where('md.documentcode = m.documentcode')
            	->where('documenttypecode = ?', $m->documenttypecode)
            	->where('categorycode = ?', $parent->getPrimaryKey())
            	->where('md.isactive = 1');
            	
           
	        	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);
            
		}
		
		static function getAssetsWithParent(FTAORMCMSAsset $parent, Zend_Db_Adapter_Abstract $zdb){
		
						
			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName), $m->fieldMap )
                ->from(array('md' => 'cmscontentmetadata'))
                ->where('md.documentcode = m.documentcode')
            	->where('documenttypecode = ?', $m->documenttypecode)
            	->where('categorycode = ?', $parent->getPrimaryKey());
	        	

			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);
            
		}
				
		static function getAssetsWithName($name, Zend_Db_Adapter_Abstract $zdb){

            if (is_null($name)){
                return array();
            }

			$myClass = get_called_class();
			
			$m = new $myClass($zdb);
			
			$zdb->setFetchMode(Zend_Db::FETCH_ASSOC);
			
			$select = $zdb->select()// we may want to simplify this.. these should be a bit smarter..
            	->from(array('m' => $m->tableName), $m->fieldMap )
                ->from(array('md' => 'cmscontentmetadata'))
                ->where('md.documentcode = m.documentcode')
            	->where('documenttypecode = ?', $m->documenttypecode)
	        	->where('title = ?', $name);
	        	
			$statement = $select->query();
			$result = $statement->fetchAll();
		
            return static::rowsToObjects($result,$myClass,$zdb);            
		}
		
		static function getAssetWithName($name, Zend_Db_Adapter_Abstract $zdb){
			$folders = static::getAssetsWithName($name, $zdb);
			if (is_array($folders) && count($folders) > 0){
				return array_pop($folders);
			} 
			return null;
		}

	}
